package com.wtg.client.control;

import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.wtg.client.api.res.StringDataRes;
import com.wtg.client.bean.User;
import com.wtg.client.feign.server.UserLoginServer;
import com.wtg.client.util.JwtUtil;

@RestController
@RequestMapping("uer")
public class UserControl {
	
	@Autowired
	private UserLoginServer userLoginServer;
	
	@RequestMapping("login")
	public StringDataRes login(String username, String password, HttpServletResponse response, HttpServletRequest request) {
		//省略登录业务
			User user = new User("132132132131","eric", "123456");
			// 3.1生成令牌
		   String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), JSONObject.toJSONString(user), null);
		   // 3.2设置jwt存入 cookie 中
		   Cookie cookie = new Cookie("Authorization", jwt);
		   response.addCookie(cookie);
		   // 3.3设置jwt存入头文件中
		   response.setHeader("Authorization", jwt);
		   StringDataRes dataRes = new StringDataRes(jwt);
		   dataRes.setMsg("登录成功");
		   dataRes.setCode("0000");
		   return dataRes;
	}
	
	//前端点击了跨平台登录
	@RequestMapping("oauthLogin/{platform}")
	public StringDataRes oauthLogin(@PathVariable(name="platform") String platform) {
		return userLoginServer.oauthLogin(platform);
	}

}
